﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Windows;
using System.Windows.Threading;
using System.Windows.Xps.Packaging;
using CodeReason.Reports;
using System.Diagnostics;
using report_demo.Data;

namespace report_demo {
    /// <summary>
    /// Interaction logic for Form40Rpt.xaml
    /// </summary>
    public partial class Form41p1Rpt : Window {
        private ReportData _reportData;
        public Form41p1Rpt() {
            InitializeComponent();
            CreateXpsDocument();
        }
        /// <summary>
        /// 
        /// </summary>
        public static void show() {
            Form41p1Rpt form = new Form41p1Rpt();
            form.Show();
        }
        private void initData() {
            DataTable table = new DataTable("report-demo");

            foreach (String cn in CustomerData.getInstance().getColumnNameArray()) {
                table.Columns.Add(cn, typeof(string));
            }
            
            for (int i = 0; i < 17; i++) {
                table.Rows.Add(new object());
            }
            _reportData.DataTables.Add(table);
        }
        /// <summary>
        /// 
        /// </summary>
        private void CreateXpsDocument() {
            try {
                ReportDocument reportDocument = new ReportDocument();

                Debug.Write(Environment.CurrentDirectory);

                StreamReader reader = new StreamReader(new FileStream(@"Templates\Form41p1Report.xaml", FileMode.Open, FileAccess.Read));
                reportDocument.XamlData = reader.ReadToEnd();
                reportDocument.XamlImagePath = Path.Combine(Environment.CurrentDirectory, @"Templates\");
                reader.Close();

                //ReportData 
                _reportData = new ReportData();

                // set constant document values
                _reportData.ReportDocumentValues.Add("PrintDate", DateTime.Now); // print date is now
                initData();


                DateTime dateTimeStart = DateTime.Now; // start time measure here

                XpsDocument xps = reportDocument.CreateXpsDocument(_reportData);
                documentViewer.Document = xps.GetFixedDocumentSequence();

                // show the elapsed time in window title
                Title += " - generated in " + (DateTime.Now - dateTimeStart).TotalMilliseconds + "ms";
            } catch (Exception ex) {
                // show exception
                MessageBox.Show(ex.Message + "\r\n\r\n" + ex.GetType() + "\r\n" + ex.StackTrace, ex.GetType().ToString(), MessageBoxButton.OK, MessageBoxImage.Stop);
            } finally {
                //busyDecorator.IsBusyIndicatorHidden = true;
            }
        }

    }
}